package thread;

public class SyncDemo1 {
    public static boolean success1 = false;//1表示取款失败
    public static boolean success2 = false;

    public static void main(String[] args) {
        Bank bank = new Bank();
        int sum = 0;
        while (true) {
            sum++;
            Thread t1 = new Thread() {
                public void run() {
                    success1 = bank.getMoney(20000);
                }
            };
            Thread t2 = new Thread() {
                public void run() {
                success2=bank.getMoney(20000);
            }
            };
            t1.start();
            t2.start();

            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();

            }
            if (success1 && success2) {//都是true说明两个人同时取款成功，出现了并发安全问题
                System.out.println("问题出现，一共尝试了" + sum + "次");
                break;
            } else {
                bank.saveAccont(20000);//重新将20000块钱存起来
                success1 = false;
                success2 = false;


            }
        }
    }
}